package com.wk.controller.student;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wk.config.ConstantProperties;
import com.wk.pojo.*;
import com.wk.pojo.Class;
import com.wk.service.Courseservice;
import com.wk.service.Userinfoservice;
import com.wk.utils.AliyunOSSUtil;
import com.wk.utils.PaginationConstant;
import com.wk.utils.responseresult;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * @program: thesis
 * @description: 学生的个人中心
 * @author: 王侃
 * @create: 2020-02-16 14:01
 **/
@Controller
@RequestMapping("/stu")
public class Stucentercontroller {

    private final org.slf4j.Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    Courseservice courseservice;

    @Autowired
    Userinfoservice userinfoservice;

    //接受Date类型数据
    @InitBinder
    protected void init(HttpServletRequest request, ServletRequestDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }

    //跳转到登录界面
    @RequestMapping("/center")
    public String center()
    {
        return "student/center";
    }

    //返回课程的类型
    @ModelAttribute("coursetypes")
    public List<Coursetype> coursetypes()
    {
        List<Coursetype> coursetypes=courseservice.selectbyalltype();
        return coursetypes;
    }

    //返回班级信息
    @ModelAttribute("classtype")
    public Collection<Class> classList()
    {
        return userinfoservice.selectbyallclass();
    }

    //显示我的桌面
    @RequestMapping("/center/desktop")
    public String desktop(Model model)
    {
        Integer number=courseservice.courtbyalluser();
        model.addAttribute("number",number);
        return "student/center_desktop";
    }

    //跳转到个人主页
    @RequestMapping("/center/myinformation")
    public String myinformation(HttpSession session,Model model)
    {
        User user=(User)session.getAttribute("loginuser");
        model.addAttribute("user",user);
        return "student/myinformation";
    }

    //跳转到头像
    @RequestMapping("/center/myinformation_icon")
    public String myinformation_icon(HttpSession session,Model model)
    {
        User user=(User)session.getAttribute("loginuser");
        model.addAttribute("user",user);
        return "student/myinformation_icon";
    }

    //更新个人头像并且提交
    @RequestMapping("/center/my_icon/updatesubmit")
    public String my_icon(HttpSession session,MultipartFile icon) throws IOException
    {
        User user=(User)session.getAttribute("loginuser");
        //将头像储存路径放置在C盘的icon文件夹中
        String path="C:\\icon";
        User_image_dto dto=new User_image_dto();
        //先将数据放入dto
        dto.setUser_id(user.getId());
        dto.setI_uploadpath(path);
        dto.setI_filename(icon.getOriginalFilename());
        dto.setI_inputStream(icon.getInputStream());
        userinfoservice.updatemyicon(dto);
        //最后在service类中，把dto交给服务类处理
        return "common/success";
    }

    //更新个人信息并且提交
    @RequestMapping("/center/myinformation/updatesubmit")
    public String myinformationupdate(User user,HttpSession session)
    {
        //进行Md5加密，第一个参数为密码，第二参数是以用户名为盐，第三个参数是加密的次数
        Md5Hash md5registepassword=new Md5Hash( user.getPassword(), user.getUsername(),5);
        user.setPassword(md5registepassword.toString());
        userinfoservice.updatebyuser(user);
        //别忘了修改session
        session.setAttribute("loginuser",user);

        return "common/success";
    }
    /**
     * 获取Object名称列表
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getlist",method = RequestMethod.GET)
    public List<String> getObjectList(){
        String bucketName = ConstantProperties.JAVA4ALL_BUCKET_NAME1;
        List<String> objectList = AliyunOSSUtil.getObjectList(bucketName);
        return objectList;
    }

    //核查修改的名称和别人的是否相同
    @RequestMapping(value = "/checkmyname",method= RequestMethod.POST,consumes="application/json")
    @ResponseBody
    public boolean checkusername(@RequestBody User user,HttpSession session)
    {
        User myuser=(User)session.getAttribute("loginuser");

        String username=user.getUsername();
        User user1=userinfoservice.selectbyusername(username);

        //如果不重名或者没有修改用户名
        if (user1==null||myuser.getUsername().equals(user.getUsername()))
            return true;
        else
            return false;
    }


}
